#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#define N 10

void worker()
{
    int a = 0;
    //a/=0;异常打断exit
    int cnt = 3;
    while(cnt--)
    {
        printf("child process, pid:%d, ppid%d\n", getpid(), getppid());
        sleep(1);
    }
}

void worker_more()
{
    int cnt = 3;
    while(cnt--)
        printf("clild process, pid:%d, ppid%d\n", getpid(), getppid());
}

void printerror()
{
    for(int i = 0; i < 134; i++)
        printf("%d: %s\n", i, strerror(i));
}

void diff_for_buffer()
{
    printf("buffer...");
    exit(0); //冲刷
    //_exit(0); 不冲刷
}

void creatSubProcess()
{
    pid_t id = fork();
    if(id == 0) 
    {
        worker();
        exit(9);
    }
    else
    {
        sleep(6);
        int status;
        pid_t rid = waitpid(id, &status, 0);

        if(rid == id)
        {
        //    printf("wait success:pid: %d, rid: %d\n", getpid(), rid);
            printf("wait success:pid: %d, rid: %d, exit sig: %d ,exit code: %d\n", getpid(), rid, status&0x7F, status>>8&0xFF);
        }
        sleep(3);
    }
    //for(int i = 0; i < n; i++)
    //{
    //    pid_t id = fork();
    //    if(id == 0)
    //    {
    //        worker();
    //        exit(0);
    //    }
    //}

}
int main()
{
    creatSubProcess();


    



    //diff_for_buffer
    //diff_for_buffer(); 

    //-11 段错误 SIGSEGV 
    //int* ptr = NULL;
    //*ptr = 4;

    //-8 SIGFPE
    //bint a = 1;
    //a /=0;

    //打印错误码对应的字符串
    //printerror();

    //for(int i = 0; i < N; i++)
    //{
    //    pid_t id = fork();
    //    if(id == 0)
    //    {
    //        worker();
    //        exit(0);
    //    }
    //}
}
